package com.spdg.ring;

import java.io.File;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.wolf.http.HttpCallBack;

import com.spdg.ring.adapter.GalleryAdapter;
import com.spdg.ring.bo.ArticleBo;
import com.spdg.ring.common.Key;
import com.spdg.ring.datamgr.ArticleDataMgr;
import com.spdg.ring.resp.ArticleResp;
import com.spdg.ring.sqlite.model.ArticleList;
import com.spdg.ring.widget.ScrollOnePageGallery;

/**
 * <p>Title:  文章详细界面</p>
 * <p>Description:  </p>
 * <p>@author: caijj , Eric.wsd    </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-22             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ArticleActivity extends RingBaseActivity {

	private TextView mTitle; // 标题
	private View mShareWeibo; // 分享
	private ScrollOnePageGallery mArticleGallery;
	private ImageView mLeftBtn;
	private ImageView mRightBtn;
	private ArticleBo articleBo;

	private GalleryAdapter mAdapter;
	private ArticleList article;
	private List<ArticleList> list;
	private String mParentKey;
	private int pos;

	@Override
	protected void initComponents() {
		mTitle = (TextView) findViewById(R.id.top_title);

		mShareWeibo = findViewById(R.id.top_right);
		mShareWeibo.setBackgroundResource(R.drawable.icon_share_weibo_bg);
		mShareWeibo.setOnClickListener(new OnShareWeiboClickLintener());

		mArticleGallery = (ScrollOnePageGallery) findViewById(R.id.article_gallery);
		mLeftBtn = (ImageView) findViewById(R.id.article_left_btn);
		mRightBtn = (ImageView) findViewById(R.id.article_right_btn);
		mLeftBtn.setOnClickListener(new OnLeftBtnClickListener());
		mRightBtn.setOnClickListener(new OnRightBtnClickListener());
	}

	@Override
	protected void initData() {
		article = (ArticleList) getIntent().getSerializableExtra(Key.K_ARTICLE_INFO); // 子列表为Item的实体类
		mParentKey = getIntent().getStringExtra(Key.K_FILE_KEY); // 子列表的父级
		pos = getIntent().getIntExtra(Key.K_CONTENT_POS, 0);
		list = ArticleDataMgr.getInstance().getArticleList(mContext, mParentKey);

		// 设置页面标题
		mTitle.setText(article.getTitle());

		// 请求详细内容数据
		articleBo = new ArticleBo(ArticleActivity.this, new GetDetailContentCallback(),
				article.getFile());
		articleBo.setParams(article.getSrc());
		articleBo.request();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mAdapter.onKeyDown()) {
				return true;
			}
		}

		return super.onKeyDown(keyCode, event);
	}

	class GetDetailContentCallback implements HttpCallBack<ArticleResp> {

		@Override
		public void call(ArticleResp response) {
			mAdapter = new GalleryAdapter(ArticleActivity.this, list);
			mArticleGallery.setAdapter(mAdapter);

			mArticleGallery.setSelection(pos);
			mArticleGallery.setOnItemSelectedListener(new OnArticleItemSelectedListener());
		}

		@Override
		public void progress(Object... obj) {
		}

		@Override
		public void onNetWorkError() {
			Toast.makeText(mContext, getString(R.string.network_error), Toast.LENGTH_SHORT).show();
		}

	}

	/**
	 * 初始化左右按钮
	 */
	private void initLeftAndRightBtn(int pos) {
		if (pos == 0) {
			mLeftBtn.setVisibility(View.INVISIBLE);
			mRightBtn.setVisibility(View.VISIBLE);
		} else if (pos == list.size() - 1) {
			mLeftBtn.setVisibility(View.VISIBLE);
			mRightBtn.setVisibility(View.INVISIBLE);
		} else {
			mLeftBtn.setVisibility(View.VISIBLE);
			mRightBtn.setVisibility(View.VISIBLE);
		}
	}

	class OnArticleItemSelectedListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			pos = position;
			initLeftAndRightBtn(position);
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}
	}

	class OnShareWeiboClickLintener implements OnClickListener {

		@Override
		public void onClick(View v) {

			String shareContent = getString(R.string.weibo_share_content, article.getName()) + article.getSharelink();
			Intent intent = new Intent(Intent.ACTION_SEND);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra(Intent.EXTRA_TEXT, shareContent); // 附带的说明信息
			intent.putExtra(Intent.EXTRA_SUBJECT, article.getSharehead());
			intent.setType("*/*");
			startActivity(Intent.createChooser(intent, article.getSharehead()));
			//
			// Intent i = new Intent(mContext, ShareActivity.class);
			// String shareContent = getString(R.string.weibo_share_content,
			// article.getName());
			// i.putExtra(Key.K_WEIBO_SHARE_CONTENT, shareContent);
			// i.putExtra(Key.K_WEIBO_SHARE_URL, article.getSharelink());
			// startActivity(i);
		}
	}

	class OnLeftBtnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (pos > 0) {
				pos -= 1;
				article = list.get(pos);

				mArticleGallery.setSelection(pos);
			}
		}
	}

	class OnRightBtnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (pos < (list.size() - 1)) {
				pos += 1;
				article = list.get(pos);

				mArticleGallery.setSelection(pos);
			}
		}
	}

	@Override
	protected int getMainContentViewId() {
		return R.layout.act_article;
	}

	@Override
	protected Class<?> getResouceClass() {
		return R.class;
	}

	@Override
	protected String getEventID() {
		return getString(R.string.event_activity_article);
	}
}
